 //
//  UnitPicker.m
//  ACMediaFrameExample
//
//  Created by 付顺龙 on 2019/3/11.
//  Copyright © 2019 ArthurCao. All rights reserved.
//

#import "UnitPicker.h"

@implementation UnitPicker

- (instancetype)init{
    self = [super init];
    if (self) {
        [self initResource];
    }
    return self;
}

- (void)initResource{
    self.isDatePicker = NO;
    
//    [self loadData];
}

-(void)loadData:(ShowBlock)block {
    [WYHXNetWorkManager post:WKDakaGetCourseHour_WKURL params:@{@"token":[UserModel getUserModel].token,@"courseId":@(self.courseId)} completionHandler:^(id responseObj, NSError *error) {
        if ([responseObj[@"code"] integerValue] == 200) {
            self.arrData = responseObj[@"data"];
            [self.pickerView reloadAllComponents];
            if (block) {
                block();
            }
        }
    }];
}
- (void)loadData {
    
//CourseHourController/queryCourseHourByCourseId
    //@"http://47.95.9.40/vlearn/CourseHourController/queryCourseHourByCourseId"
    [WYHXNetWorkManager post:WKDakaGetCourseHour_WKURL params:@{@"token":[UserModel getUserModel].token,@"courseId":@(self.courseId)} completionHandler:^(id responseObj, NSError *error) {
        if ([responseObj[@"code"] integerValue] == 200) {
            self.arrData = responseObj[@"data"];
            [self.pickerView reloadAllComponents];
        }
    }];
}
-(void)setCourseId:(NSInteger )courseId {
    _courseId = courseId;
    [self loadData];
}

- (id)getSelectData:(NSArray*)arrSelect{

    if (!arrSelect || arrSelect.count == 0) {
        return _arrData[0];
    }
    NSDictionary* dic = arrSelect[0];
    NSNumber* row = [dic objectForKey:KEY_ROW];
    if (row.intValue > _arrData.count) {
        return _arrData[0];
    }
    return _arrData[row.intValue];
}

- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView{
    return 1;
}
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component{
    return self.arrData.count;
}
- (CGFloat)pickerView:(UIPickerView *)pickerView widthForComponent:(NSInteger)component{
    NSInteger num = [self numberOfComponentsInPickerView:pickerView];
    return ScreenW/num;
}

//- (nullable NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component{
//    return @"男";
//}
- (nullable NSAttributedString *)pickerView:(UIPickerView *)pickerView attributedTitleForRow:(NSInteger)row forComponent:(NSInteger)component{
    NSDictionary*unit = _arrData[row];
    
    NSMutableAttributedString* text = [[NSMutableAttributedString alloc] initWithString:unit[@"hourName"]];
    return text;
}

- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component{
    
}

- (void)setDefaultSelect:(int)row{
    [self.pickerView selectRow:row inComponent:0 animated:YES];
}


@end
